//
//  SSSVDiscountViewController.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/27.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSDiscountViewController.h"
#import "SSSWaitDiscountViewController.h"
#import "SSSCompleteViewController.h"
#import "SSSProcessedingViewController.h"
#import "SSSCompleteViewController.h"
#import "UIView+DDAdd.h"

@interface SSSDiscountViewController ()

{
    
    UILabel *_orderNuLabel;
    UILabel *_label1;
    UILabel *_discountNuLabel;
    UILabel *_label2;
    UILabel *_allMoneyNuLabel;//订单金额
    UILabel *_oderNuLabel;//订单数量
    SSSWaitDiscountViewController *waitVc;
    SSSProcessedingViewController *ProcessedingVc;
    SSSCompleteViewController *completeVc;
    NSInteger _currentPage;//当前页数
}
/** 下方选择selectedBtn */
@property (nonatomic, strong) UIButton *selectedBtn;

/** currentBtn */
@property (nonatomic, strong) UIButton *currentBtn;
/** 底部白色指示器 */
@property(nonatomic, weak) UIView *indicatorView;

@end

@implementation SSSDiscountViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
    self.title = @"订 单";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //添加子控制器
    [self addChildViewControllers];
    //初始化界面
    [self setUpApperance];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendWaiting:) name:@"sendWaitingNuStr" object:nil];
}

- (void)sendWaiting:(NSNotification *)notification{
    
    NSString *str = [NSString stringWithFormat:@"%@",notification.userInfo[@"allMoney"]];
    _orderNuLabel.text = str;
    NSString *disStr = [NSString stringWithFormat:@"%@",notification.userInfo[@"rangli"]];
    _discountNuLabel.text = disStr;
}
- (void)sendProcesseding:(NSNotification *)notification{
    
    NSString *str = [NSString stringWithFormat:@"%@",notification.userInfo[@"allMoney"]];
    
    _orderNuLabel.text = str;
    NSString *disStr = [NSString stringWithFormat:@"%@",notification.userInfo[@"rangli"]];
    _discountNuLabel.text = disStr;
}
- (void)sendCompleteNuStr:(NSNotification *)notification{
    
    NSString *str = [NSString stringWithFormat:@"%@",notification.userInfo[@"allMoney"]];
    
    _orderNuLabel.text = str;
    NSString *disStr = [NSString stringWithFormat:@"%@",notification.userInfo[@"rangli"]];
    _discountNuLabel.text = disStr;
}

- (void)setUpApperance{
    
    UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, KSCREEN_Width, HeigthIos6CompeteWithCurrent(119))];
    headerImageView.userInteractionEnabled = YES;
    headerImageView.image = [UIImage imageNamed:@"img_bg"];
    [self.view addSubview:headerImageView];
    
    UIView *titilesView = [[UIView alloc] initWithFrame:CGRectMake(0, HeigthIos6CompeteWithCurrent(10),self.view.width,HeigthIos6CompeteWithCurrent(30))];
    // 设置透明度
    titilesView.backgroundColor = [UIColor clearColor];
    [headerImageView addSubview:titilesView];
    
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor colorWithHex:@"FFFFFF"];
    indicatorView.height = 2;
    indicatorView.y = titilesView.height - indicatorView.height;
    [titilesView addSubview:indicatorView];
    self.indicatorView = indicatorView;

    
    
    //三个按钮
    NSArray *titleArray = @[@"待让利",@"处理中",@"已完结"];
    CGFloat titleWidth = KSCREEN_Width/3;
    CGFloat titleHeight = HeigthIos6CompeteWithCurrent(30);
    for (int index = 0; index < titleArray.count; index++) {
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(index *titleWidth, HeigthIos6CompeteWithCurrent(0), titleWidth, titleHeight)];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        btn.tag = index + 2000;
        [btn setTitle:titleArray[index] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHex:@"9FBAB4"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHex:@"FFFFFF"] forState:UIControlStateDisabled];
        btn.titleLabel.font = [SSSFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        [titilesView addSubview:btn];
        //默认显示第一个
        if(index == 0){
            [self.view addSubview:self.childViewControllers[0].view];
            btn.enabled = NO;
            self.currentBtn = btn;
            // 让按钮那边的label根据文字内容计算尺寸
            [btn.titleLabel sizeToFit]; // 方法1
            self.indicatorView.width = btn.titleLabel.width;
            self.indicatorView.centerX = btn.centerX;

        }
    }
    
    //添加白色视图
    UIView *showHeaderView = [[UIView alloc]init];
    showHeaderView.backgroundColor = [UIColor whiteColor];
    [headerImageView addSubview:showHeaderView];
    showHeaderView.sd_layout
    .topSpaceToView(headerImageView,HeigthIos6CompeteWithCurrent(59))
    .leftSpaceToView(headerImageView,WidthIos6CompeteWithCurrent(12))
    .rightSpaceToView(headerImageView,WidthIos6CompeteWithCurrent(12))
    .heightIs(HeigthIos6CompeteWithCurrent(116));
    showHeaderView.layer.cornerRadius = 10;
    showHeaderView.layer.masksToBounds = YES;
    
    //line
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor colorWithHex:@"D9F1E5"];
    [showHeaderView addSubview:lineView];
    lineView.sd_layout
    .centerXEqualToView(showHeaderView)
    .topSpaceToView(showHeaderView,HeigthIos6CompeteWithCurrent(20))
    .bottomSpaceToView(showHeaderView,HeigthIos6CompeteWithCurrent(20))
    .widthIs(1);
    
    //订单总额
    _orderNuLabel = [UILabel new];
    [showHeaderView addSubview:_orderNuLabel];
    _orderNuLabel.text = @"0.00";
    
    _orderNuLabel.textColor = [UIColor colorWithHex:@"40B87D"];
    _orderNuLabel.font = [SSSFont systemFontOfSize:35];
    _orderNuLabel.textAlignment = NSTextAlignmentCenter;
    _orderNuLabel.sd_layout
    .leftSpaceToView(showHeaderView,0)
    .rightEqualToView(lineView)
    .topSpaceToView(showHeaderView,HeigthIos6CompeteWithCurrent(31))
    .heightIs(HeigthIos6CompeteWithCurrent(27));
    
    
    
    _label1 = [UILabel new];
    [showHeaderView addSubview:_label1];
    _label1.text = @"订单总额";
    _label1.textColor = [UIColor colorWithHex:@"999999"];
    _label1.font = [SSSFont systemFontOfSize:12];
    _label1.textAlignment = NSTextAlignmentCenter;
    _label1.sd_layout
    .leftSpaceToView(showHeaderView,0)
    .rightEqualToView(lineView)
    .bottomSpaceToView(showHeaderView,HeigthIos6CompeteWithCurrent(26))
    .heightIs(HeigthIos6CompeteWithCurrent(12));
    
    //让利金额
    _discountNuLabel = [UILabel new];
    [showHeaderView addSubview:_discountNuLabel];
    _discountNuLabel.text = @"65";
    _discountNuLabel.textColor = [UIColor colorWithHex:@"40B87D"];
    _discountNuLabel.font = [SSSFont systemFontOfSize:35];
    _discountNuLabel.textAlignment = NSTextAlignmentCenter;
    _discountNuLabel.sd_layout
    .leftSpaceToView(lineView,0)
    .rightEqualToView(showHeaderView)
    .topSpaceToView(showHeaderView,HeigthIos6CompeteWithCurrent(31))
    .heightIs(HeigthIos6CompeteWithCurrent(27));
    
    _label2 = [UILabel new];
    [showHeaderView addSubview:_label2];
    _label2.text = @"未让利订单数";
    _label2.textColor = [UIColor colorWithHex:@"999999"];
    _label2.font = [SSSFont systemFontOfSize:12];
    _label2.textAlignment = NSTextAlignmentCenter;
    _label2.sd_layout
    .leftSpaceToView(lineView,0)
    .rightEqualToView(showHeaderView)
    .bottomSpaceToView(showHeaderView,HeigthIos6CompeteWithCurrent(26))
    .heightIs(HeigthIos6CompeteWithCurrent(12));
    
}
- (void)btnClick:(UIButton *)button{
    
    self.currentBtn.enabled = YES;
    button.enabled = NO;
    self.currentBtn = button;
    
    // 动画
    [UIView animateWithDuration:0.25 animations:^{
        // 适应宽度
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];

    switch (self.currentBtn.tag - 2000) {
        case 0:
            _label1.text = @"订单总额";
            _label2.text = @"未让利订单数";
            [self.view addSubview:self.childViewControllers[0].view];
            //注册通知
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendWaiting:) name:@"sendWaitingNuStr" object:nil];
            [completeVc.view removeFromSuperview];
            [ProcessedingVc.view removeFromSuperview];
            break;
        case 1:
            _label1.text = @"订单总额";
            _label2.text = @"应让利";
            [self.view addSubview:self.childViewControllers[1].view];
            
            //注册通知
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendProcesseding:) name:@"sendProcessedingNuStr" object:nil];
            [waitVc.view removeFromSuperview];
            [completeVc.view removeFromSuperview];
            break;
        case 2:
            _label1.text = @"订单总额";
            _label2.text = @"已让利";
            [self.view addSubview:self.childViewControllers[2].view];
            //注册通知
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendCompleteNuStr:) name:@"sendCompleteNuStr" object:nil];
            [ProcessedingVc.view removeFromSuperview];
            [waitVc.view removeFromSuperview];
            break;
        default:
            break;
    }
    
}
- (void)allSelectedBtnClick:(UIButton *)sender{
    
    
    sender.selected = !sender.selected;
    
}
- (void)addChildViewControllers{
    
    waitVc = [[SSSWaitDiscountViewController alloc]init];
    [self addChildViewController:waitVc];
    waitVc.view.frame = CGRectMake(0, HeigthIos6CompeteWithCurrent(59)+HeigthIos6CompeteWithCurrent(116)+64 , KSCREEN_Width, KSCREEN_Height - 64 - HeigthIos6CompeteWithCurrent(59) - HeigthIos6CompeteWithCurrent(116));
    
    ProcessedingVc = [[SSSProcessedingViewController alloc]init];
    [self addChildViewController:ProcessedingVc];
    ProcessedingVc.view.frame = CGRectMake(0, HeigthIos6CompeteWithCurrent(59)+HeigthIos6CompeteWithCurrent(116) +64, KSCREEN_Width, KSCREEN_Height - 64 - HeigthIos6CompeteWithCurrent(59) - HeigthIos6CompeteWithCurrent(116));
    
    completeVc = [[SSSCompleteViewController alloc]init];
    [self addChildViewController:completeVc];
    completeVc.view.frame = CGRectMake(0, HeigthIos6CompeteWithCurrent(59)+HeigthIos6CompeteWithCurrent(116) +64, KSCREEN_Width, KSCREEN_Height - 64 - HeigthIos6CompeteWithCurrent(59) - HeigthIos6CompeteWithCurrent(116));
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
